<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/12 0012
 * Time: 下午 3:55
 */

namespace ServiceClass;

use Illuminate\Support\Facades\DB;
use ServiceClassInterface\AdminContract;
use ToolClass\PageClass;

class Admin implements AdminContract
{
    public function addAdmin($adminData)
    {
        $adminName = $adminData['admin_name'];

        $check = DB::table('admin')->where('admin_name','=',$adminName)->first();

        if($check){
            return "该用户名已被注册";
        }

        $adminData['admin_CreateTime'] = date('Y-m-d');
        $adminData['admin_password']   = md5($adminData['admin_password']);
        $result = DB::table('admin')->insert($adminData);

        if($result){
            return 1;
        }else{
            return "服务器错误，请重试";
        }
    }

    public function deleteAdmin($adminData)
    {

        $result = DB::table('admin')->where('admin_id',$adminData)->delete();
        if($result){
            return 1;
        }else{
            return 0;
        }
    }

    public function searchAdmin($adminData)
    {

        $result = DB::table('admin')->where('admin_id',$adminData)->get();
        if($result){
            return $result;
        }else{
            return 0;
        }
    }

    public function selectAdminList($table,$limit,$where,$page)
    {
        $result = PageClass::Page($table,$limit,$where,$page);

        return $result;

    }

    public function updateAdmin($adminData)
    {
        $admin_id = $adminData['admin_id'];

        $result = DB::table('admin')->where('admin_id',$admin_id)->update([
            'admin_password' => md5($adminData['admin_password'])
        ]);

        if($result){
            return 1;
        }else{
            return "服务器错误，请重试";
        }
    }
}